//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package api_common

import (
	"context"
	"time"

	"atomgit.com/openlinksaas-org/api-server/dao/event_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/notices/notices_project"
	"google.golang.org/protobuf/proto"
)

func AddEvent(ctx context.Context, event *events_api.Event, skipUserIndex bool, eventSourceId, sourceUserName string) error {
	//写入event
	eventData, err := proto.Marshal(event.EventData)
	if err != nil {
		return err
	}
	err = event_dao.EventInfoDao.Insert(ctx, &event_dao.EventInfoDbItem{
		EventId:   event.EventId,
		EventData: eventData,
	})
	if err != nil {
		return err
	}
	//更新project index
	if event.ProjectId != "" {
		err = event_dao.ProjectEventIndexDao.Insert(ctx, &event_dao.ProjectEventIndexDbItem{
			ProjectId:       event.ProjectId,
			ProjectName:     event.ProjectName,
			UserId:          event.UserId,
			UserDisplayName: event.UserDisplayName,
			EventId:         event.EventId,
			EventSourceId:   eventSourceId,
			SourceUserName:  sourceUserName,
			EventType:       uint32(event.EventType),
			RefType:         uint32(event.RefType),
			RefId:           event.RefId,
			EventTime:       event.EventTime,
		})
		if err != nil {
			return err
		}
	}
	eventTime := time.Unix(event.EventTime/1e3, 0)
	//更新project day status
	day := eventTime.Year()*10000 + int(eventTime.Month())*100 + eventTime.Day()
	if event.ProjectId != "" {
		memberUserIdList := []string{""}
		if event.UserId != "" {
			memberUserIdList = append(memberUserIdList, event.UserId)
		}
		for _, memberUserId := range memberUserIdList {
			exist, err := event_dao.ProjectDayEventStatusDao.Exist(ctx, event.ProjectId, memberUserId, uint32(day), event.EventType)
			if err != nil {
				return err
			}
			if !exist {
				err = event_dao.ProjectDayEventStatusDao.Insert(ctx, &event_dao.ProjectDayEventStatusItemDbItem{
					ProjectId:    event.ProjectId,
					MemberUserId: memberUserId,
					Day:          uint32(day),
					EventType:    int32(event.EventType),
				})
				if err != nil {
					return err
				}
			}
			err = event_dao.ProjectDayEventStatusDao.Inc(ctx, event.ProjectId, memberUserId, uint32(day), int32(event.EventType))
			if err != nil {
				return err
			}
		}
	}
	//更新last event id
	if event.UserId != "" && !skipUserIndex {
		err = project_dao.MemberInfoDao.SetLastEventId(ctx, event.ProjectId, event.UserId, event.EventId)
		if err != nil {
			return err
		}
	}
	if event.ProjectId != "" {
		//发送通知
		if !skipUserIndex { //特殊事件不做处理
			go func() {
				//延迟200ms发送
				time.Sleep(200 * time.Millisecond)
				newCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
				defer cancel()
				SendNoticeToProjectScope(newCtx, event.ProjectId, &notices_project.NewEventNotice{
					ProjectId:    event.ProjectId,
					MemberUserId: event.UserId,
					EventId:      event.EventId,
				})
			}()
		}
	}
	return nil
}
